package com.cryptape.cita_wallet.activity;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.alm.token.R;
import com.cryptape.cita_wallet.bean.MangoAssociation;
import com.cryptape.cita_wallet.bean.MangoAssociationBean;
import com.cryptape.cita_wallet.service.http.TokenService;
import com.cryptape.cita_wallet.view.CompressEditText;

import java.util.List;

import rx.Subscriber;

;

/**
 * Created by dengming on 13/1/20.
 */

public class MangoAssociationActivity extends NBaseActivity{
    private RecyclerView recyclerView;
    private Adapter mAdapter;
    private TextView tv_yesterdayPool;
    private TextView tv_soCalculateForce_value;
    private TextView tv_shareTheWorkForce_value;
    private TextView teamCalculateForce;
    private TextView tv_hashrate_yesterday;
    private TextView tv_yesterdayStimulate_value;
    private TextView tv_yesterdaySoCalculateForce_value;
    private MangoAssociationBean mData;
    private Button advanced_setup_button;
    private CompressEditText cet_address;
    private TextView tv_nowPool;
//    private List<MangoItemBean> mData = new ArrayList<>();

    @Override
    protected int getContentLayout() {
        return R.layout.activity_mango_aassociation;
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.mango_recycler);
        tv_yesterdayPool = findViewById(R.id.tv_yesterdayPool);
        tv_soCalculateForce_value = findViewById(R.id.tv_network_p1);
        tv_shareTheWorkForce_value = findViewById(R.id.tv_shareTheWorkForce_value);
        teamCalculateForce  = findViewById(R.id.tv_shareTheWorkForce_value);
        tv_yesterdaySoCalculateForce_value = findViewById(R.id.tv_yesterdaySoCalculateForce_value);
        tv_hashrate_yesterday  = findViewById(R.id.tv_hashrate_yesterday);
//        tv_yesterdayForce_value = findViewById(R.id.tv_yesterdayForce_value);
        tv_yesterdayStimulate_value = findViewById(R.id.tv_yesterdayStimulate_value);
//        mData.add(new MangoItemBean("MID11111","0x111111111111111"));
//        mData.add(new MangoItemBean("MID11111","0x111111111111111"));
//        mData.add(new MangoItemBean("MID11111","0x111111111111111"));
        advanced_setup_button  = (Button) findViewById(R.id.advanced_setup_button);
        cet_address  =  findViewById(R.id.cet_address);
        tv_nowPool = (TextView)findViewById(R.id.tv_nowPool);
        initAdapter();
    }

    @Override
    protected void initData() {
        TokenService.getTokgetMangoAssociationenCurrency("").subscribe(new Subscriber<MangoAssociationBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(MangoAssociationBean s) {
                if(s != null){
                    mData = s;
                    if(s.getCode() != 0){
                        return;
                    }
                    if(mAdapter != null) {
                        mAdapter.notifyDataSetChanged();
                    }
                    tv_nowPool.setText(mData.getData().getTodayCashIncentivePool());
                    tv_yesterdayPool.setText(getString(R.string.Yesterday_pool)+mData.getData().getYesterdayCashIncentivePool());
                    tv_soCalculateForce_value.setText(mData.getData().getSoCalculateForce());
                    tv_shareTheWorkForce_value.setText(mData.getData().getShareTheWorkForce());
                    teamCalculateForce.setText(mData.getData().getTeamCalculateForce());
                    tv_yesterdaySoCalculateForce_value.setText(getString(R.string.Power)+mData.getData().getYesterdaySoCalculateForce());
                    tv_yesterdayStimulate_value.setText(getString(R.string.Bonus)+mData.getData().getYesterdayStimulate());
                    tv_hashrate_yesterday.setText(getString(R.string.hashrate_yesterday)+mData.getData().getYesterdayForce());
                }
            }
        });
    }

    @Override
    protected void initAction() {
        advanced_setup_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(cet_address.getText())){
                    Toast.makeText(MangoAssociationActivity.this,"请输入MID",Toast.LENGTH_SHORT).show();
                    return;
                }
                TokenService.getTokgetMangoAssociationenCurrency(cet_address.getText()).subscribe(new Subscriber<MangoAssociationBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(MangoAssociationActivity.this,getString(R.string.search_failed),Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onNext(MangoAssociationBean s) {
                        if(s != null){
                            if(mData.getCode() != 0){
                                Toast.makeText(MangoAssociationActivity.this,getString(R.string.search_failed),Toast.LENGTH_SHORT).show();

                                return;
                            }
                            mData = s;
                            if(mAdapter != null) {
                                mAdapter.notifyDataSetChanged();
                            }
//                            tv_nowPool.setText(mData.getData().getTodayCashIncentivePool());
//                            tv_cashIncentivePool_value.setText(mData.getData().getYesterdayCashIncentivePool());
//                            tv_soCalculateForce_value.setText(mData.getData().getSoCalculateForce());
//                            tv_shareTheWorkForce_value.setText(mData.getData().getShareTheWorkForce());
//                            teamCalculateForce.setText(mData.getData().getTeamCalculateForce());
//                            tv_yesterdaySoCalculateForce_value.setText(getString(R.string.Power)+mData.getData().getYesterdaySoCalculateForce());
//                            tv_yesterdayStimulate_value.setText(getString(R.string.Bonus)+mData.getData().getYesterdayStimulate());
//                            tv_hashrate_yesterday.setText(getString(R.string.hashrate_yesterday)+mData.getData().getYesterdayForce());
                            tv_nowPool.setText(mData.getData().getTodayCashIncentivePool());
                            tv_yesterdayPool.setText(getString(R.string.Yesterday_pool)+mData.getData().getYesterdayCashIncentivePool());
                            tv_soCalculateForce_value.setText(mData.getData().getSoCalculateForce());
                            tv_shareTheWorkForce_value.setText(mData.getData().getShareTheWorkForce());
                            teamCalculateForce.setText(mData.getData().getTeamCalculateForce());
                            tv_yesterdaySoCalculateForce_value.setText(getString(R.string.Power)+mData.getData().getYesterdaySoCalculateForce());
                            tv_yesterdayStimulate_value.setText(getString(R.string.Bonus)+mData.getData().getYesterdayStimulate());
                            tv_hashrate_yesterday.setText(getString(R.string.hashrate_yesterday)+mData.getData().getYesterdayForce());
                        }else{
                            Toast.makeText(MangoAssociationActivity.this,"搜索失败",Toast.LENGTH_SHORT).show();

                        }
                    }
                });
            }
        });
    }

    private void initAdapter() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new Adapter();
        recyclerView.setAdapter(mAdapter);
    }

    private class Adapter extends RecyclerView.Adapter<ViewHolder> {

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(MangoAssociationActivity.this)
                    .inflate(R.layout.item_mango_id, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            List<MangoAssociation.MIDItem>  data = mData.getData().getData();
            if(!TextUtils.isEmpty(data.get(position).getLevelName())) {
                holder.key.setText(data.get(position).getMid() + "[" + data.get(position).getLevelName() + "]");
            }else{
                holder.key.setText(data.get(position).getMid());
            }
            holder.value.setText(data.get(position).getChildAddress());
        }

        @Override
        public int getItemCount() {
            if(mData == null){
                return 0;
            }
            if(mData.getData() == null){
                return 0;
            }

            if(mData.getData().getData() == null){
                return 0;
            }
            return mData.getData().getData().size();
        }
    }

    private class ViewHolder extends RecyclerView.ViewHolder {

        private TextView key, value;

        public ViewHolder(View itemView) {
            super(itemView);
            key = itemView.findViewById(R.id.tv_title);
            value = itemView.findViewById(R.id.tv_value);
        }
    }
}
